/**
 *
 */
package com.oceansoft.mobile.econsole.modules.advice.service.impl;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.cache.redis.CacheBaseService;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.advice.entity.Advice;
import com.oceansoft.mobile.econsole.modules.advice.persistence.IAdviceMapper;
import com.oceansoft.mobile.econsole.modules.advice.service.IAdviceService;
import com.oceansoft.mobile.econsole.modules.apps.entity.AppDownload;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.log.entity.AccessLog;
import com.oceansoft.mobile.econsole.modules.message.entity.PushMessage;
import com.oceansoft.mobile.econsole.modules.message.util.Utils;
import com.oceansoft.mobile.econsole.task.sms.SMSUtil;
import com.oceansoft.mobile.econsole.task.sms.ScheduleTasker;
import com.oceansoft.mobile.econsole.task.sms.StringUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 意见投诉Service实现
 *
 * @author jin.xu
 * @version 2014-08-06
 */
@Service
@Transactional
public class AdviceServiceImpl extends CacheBaseService implements IAdviceService {
    @Resource
    private IAdviceMapper adviceMapper;

    @Override
    public Pager<Advice> findAdviceByPage(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<Advice> page = (Page<Advice>) adviceMapper.findAdviceListByPage(query);
        return PageUtil.page2pager(page, query);
    }
    
    
    @Override
	public List<Advice> loadExcel(Query query){
		List<Advice> list=adviceMapper.findExeclByCondition(query);
		return list;
	} 

    @Override
    public Advice findAdviceById(int adviceId) {
        return adviceMapper.findAdviceById(adviceId);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Result deleteAdviceById(int adviceId) {
        adviceMapper.deleteAdviceById(adviceId);
        return new Result();
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Result save(Advice advice) {
        int id = adviceMapper.save(advice);
        advice.setId(id);
        return new Result(advice);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Result update(Advice advice) {
        adviceMapper.update(advice);
        return new Result();
    }
    
    
    @Override
    public Result updateStatus(Advice advice){
    	 adviceMapper.updateStatus(advice);
    	 return new Result(Result.SUCC, "更新成功");
    }
    
    
    @Override
    public Result pushBackInfo(Advice advice) {
        try {
        	String mobile=advice.getLoginId();
        	String reg="^1\\d{10}$";
        	boolean p=false;
        	if(StringUtil.isNotNull(mobile)){
        	 p = Pattern.compile(reg).matcher(mobile).find();
        	}
        	String contactor=advice.getContact();
        	boolean c=false;
        	if(StringUtil.isNotNull(contactor)){
        		c=Pattern.compile(reg).matcher(contactor).find();
           	}
        	
        	String mess="针对您提出对于公安手机App的反馈意见,我们的回复是"+advice.getDescription();
        	if(StringUtil.isNotNull(mobile)&p){
          JSONObject json = new JSONObject();
          json.put("你好", mess);
//            json.put("CL", driverInfoApiMapper.loadVehicleBindInfo(sfzh));
//            json.put("JSZ", driverInfoApiMapper.loadDriverBindInfo(sfzh));
            //构造PushMessage
            //PushMessage message=new PushMessage(500001835, 3, "pap_"+mobile, json.toJSONString(), "自动推送");
            PushMessage message = new PushMessage(500001835, 3, "pap_" + mobile, "{\"content_type\":-1,\"extras\":\"\",\"title\":\"反馈信息回复\",\"message\":" + json.toJSONString()+ "}", "自动推送");
           
            //System.out.println("==============json================"+"{\"content_type\":8,\"extras\":\"\",\"title\":\"换证通知\",\"message\":"+json.toJSONString()+"}");
            String resp = Utils.httpPost(configInfo.getPushServerHost(), null, Utils.toParamFromBean(message));
       //     System.out.println("===============resp=============="+resp);
         SMSUtil.sendSMS(advice.getLoginId(),mess,advice.getLoginId(),"325");

       //     new ScheduleTasker(advice.getLoginId(), new Date(), 600, configInfo, advice, adviceMapper).startTask();
            advice.setHandler("2");
            
          //  advice.setCurrentTime(new Date());
            adviceMapper.update(advice);
            return new Result(Result.SUCC, "反馈消息和短信数据推送成功");
        	}
        	else if(StringUtil.isNotNull(contactor)&c){
        		 SMSUtil.sendSMS(advice.getContact(),mess,advice.getContact(),"325");
        		advice.setHandler("1");
        		
        		
        		adviceMapper.update(advice);
        		 return new Result(Result.SUCC, "短信数据推送成功");
        	}else{
        		advice.setHandler("0");
        	
        		adviceMapper.update(advice);
        		return new Result(Result.SUCC,"登陆账号为空并且联系方式是QQ或者邮箱,不能回复意见");
        	}
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "信息推送失败");
        }
		
    }
    
}
